package edu.jhu.tcj.purfuit;

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 
 * @author Brad Gabrielse
 *
 */
public class Category {

	/** Category ID */
	private int ID;
	/** Category name */
	private String name;
	/** Associated color */
	private Color color;
	
	/** Default constructor */
	public Category() {
		
	}
	
	/**
	 * @param iD the iD to set
	 */
	public void setID(int iD) {
		ID = iD;
	}

	/**
	 * @return the iD
	 */
	public int getID() {
		return ID;
	}

	/**
	 * @return the name of the category
	 */
	public String getName() {
		return name;
	}

	/**
	 * Set the category name
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the color of the category
	 */
	public Color getColor() {
		return color;
	}

	/**
	 * Set the category color
	 * @param color
	 */
	public void setColor(Color color) {
		this.color = color;
	}

	public String toString() {
		return name;
	}
	

	/** 
	 * Load the categories from a configuration file
	 * @return an array of categories
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static Category[] loadCategories() throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		FileInputStream fileInputStream = new FileInputStream("conf/Categories.properties");
		properties.load(fileInputStream);
		fileInputStream.close();
		String[] categoriesStr = properties.getProperty("categories").split(",");
		Category[] categories = new Category[categoriesStr.length];
		for (int i = 0; i < categoriesStr.length; i++) {
			categories[i] = new Category();
			categories[i].setID(Integer.parseInt(properties.getProperty(categoriesStr[i] + ".ID")));
			categories[i].setName(properties.getProperty(categoriesStr[i] + ".type", "Undefined"));
			String[] colorStr = properties.getProperty(categoriesStr[i] + ".color").split(",");
			int red = Integer.parseInt(colorStr[0]);
			int green = Integer.parseInt(colorStr[1]);
			int blue = Integer.parseInt(colorStr[2]);
			categories[i].color = new Color(red,green,blue);
		}
		return categories;
	}
	
	/**
	 * Saves the categories to a configuration file
	 * @param categories
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void saveCategories(Category[] categories) throws FileNotFoundException, IOException {
		Properties properties = new Properties();
		String categoryList = "";
		for (int i = 0; i < categories.length; i++) {
			String name = categories[i].getName();
			properties.setProperty("category" + (i+1) + ".type", name);
			Color color = categories[i].getColor();
			properties.setProperty("category" + (i+1) + ".color", color.getRed() + "," + color.getGreen() + "," + color.getBlue());
			categoryList += "category" + (i+1) + ",";
		}
		properties.setProperty("categories",categoryList);
		FileOutputStream fileOutputStream = new FileOutputStream("conf/Categories.properties");
		properties.store(fileOutputStream,"Trivial Pursuit Category Information");
		fileOutputStream.close();
	}
}
